package com.u17;


import java.util.ArrayList;

/**
 * @author u17
 * https://leetcode-cn.com/problems/add-binary/
 */
public class Solution {

    public String addBinary(String a, String b) {

        int aIndex = a.length() - 1;

        int bIndex = b.length() - 1;

        boolean jinWei = false;

        StringBuilder result = new StringBuilder();

        while (aIndex >= 0 || bIndex >= 0){

            int total = 0;

            if(aIndex >= 0 && a.charAt(aIndex--) == '1'){
                total++;
            }

            if(bIndex >= 0 && b.charAt(bIndex--) == '1'){
                total++;
            }

            if(jinWei) {
                total++;
            }

            jinWei = total > 1 ? true : false;

            result.append(total % 2);
        }

        if(jinWei) {
            result.append(1);
        }

        return result.reverse().toString();
    }


    public static void main(String[] args) {
        Solution solution = new Solution();
        String a = "11";
        String b = "1";
        System.out.println(solution.addBinary(a, b));
    }

}
